﻿using MyMES.Domain.W.Liststructure;
using MyMES.Domain.yjb;
using MyMES.Infrastructure.Interface;
using Org.BouncyCastle.Asn1.Ocsp;
using WorkflowCore.Interface;
using WorkflowCore.Models;

namespace MyMES.Writer.API.Workflow
{
    public class CompletionApprovalWorkflowd : IWorkflow<Approval>
    {
        public string Id => "CompletionApprovalWorkflowd";

        public int Version => 1;

        public void Build(IWorkflowBuilder<Approval> builder)
        {
            builder
                .Saga(saga => saga.StartWith<TicketApproval>()
                .Input(x=>x.ModelId,data=>data.ModelId)
                .Input(x=>x.LeaderId,data=>data.LeaderId)
                .Input(x=>x.LeaderName,data=>data.LeaderName)
                .Input(x=>x.AppResult,data=>data.AppResult)
                .Input(x=>x.Reason,data=>data.Reason)
                .Input(x=>x.CreateTime,data=>DateTime.Now)
            //.Then<TicketApproval12>()
            //.Input(x=>x.ModelId,data=>data.ModelId)
            //.Input(x=>x.AppResult,data=>data.AppResult)
                );
        }

        public class TicketApproval : StepBody
        {
            private readonly IBaseRepository<Approval> accreop;

            public TicketApproval(IBaseRepository<Approval> accreop)
            {
                this.accreop = accreop;
            }

            public int ModelId { get; set; }
            public int LeaderId { get; set; }
            public string LeaderName { get; set; }
            public int AppResult { get; set; }
            public string? Reason { get; set; }
            public DateTime? CreateTime { get; set; }

            public override ExecutionResult Run(IStepExecutionContext context)
            {
               var model=new Approval()
               {
                   ModelId=ModelId,
                   LeaderId=LeaderId,
                   LeaderName=LeaderName,
                   AppResult=AppResult,
                   Reason=Reason,
                   CreateTime=CreateTime
               };
                accreop.Insert(model);
                Console.WriteLine("初始化请求");
                return ExecutionResult.Next();
            }
        }

        public class TicketApproval2:StepBody
        {
            private readonly IBaseRepository<ProductionReport> accreops;

            public TicketApproval2(IBaseRepository<ProductionReport> accreops)
            {
                this.accreops = accreops;
            }
            public int AppResult { get; set; }
            public int ModelId { get; set; }

            public override ExecutionResult Run(IStepExecutionContext context)
            {
                Console.WriteLine("审批状态");
                //var sql = $"Update ProductionReport set Status=3 where ProductionReportId='{ProductionReport.ProductionReportId}'";
                //var o = accreops.GetList<ProductionReport, sql>().First(x => x.ProductionReportId = ModelId);
                throw new NotImplementedException();
            }
        } 
    }
}
